
//
//  EditAddressListViewController.m
//  LuxuryGoods
//
//  Created by DCQ on 2018/5/25.
//  Copyright © 2018年 Future. All rights reserved.
//

#import "EditAddressListViewController.h"
#import "EditAddressCell.h"
#import "EditAddressFooterView.h"
#import "ChooseAddressViewController.h"
#import "AddressListModel.h"
#import "UserInfoModel.h"
NSString * const EditAddressCellIdentifier = @"EditAddressCellIdentifier";
NSString * const EditAddressFooterViewIdentifier = @"EditAddressFooterViewIdentifier";
@interface EditAddressListViewController ()
@property(nonatomic,assign) AddressListStyle style;
@property(nonatomic, strong)AddressModel *addressModel;
@property(nonatomic, copy)NSString *consignee;
@property(nonatomic, copy)NSString *phone;
@property(nonatomic, copy)NSString *location;
@property(nonatomic, copy)NSString *detail_address;

@end

@implementation EditAddressListViewController
- (instancetype)initWithData:(id)data WithStyle:(AddressListStyle )style{
    self = [super init];
    if (self) {
        self.style = style;
        self.addressModel = data;
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    NSString * titleStr = @"";
    if (self.style== AddressListStyle_new) {
        titleStr = @"添加地址";
    }else if (self.style == AddressListStyle_edit){
        titleStr = @"修改地址";
        self.consignee = self.addressModel.consignee;
        self.detail_address = self.addressModel.detail_address;
        self.phone = self.addressModel.phone;
        self.location = self.addressModel.location;
        self.detail_address = self.addressModel.detail_address;
    }
    [self setNavigationItemTitle: titleStr];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (void)registerCell{
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([EditAddressCell class]) bundle:nil] forCellReuseIdentifier:EditAddressCellIdentifier];
    
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([EditAddressFooterView class]) bundle:nil] forHeaderFooterViewReuseIdentifier:EditAddressFooterViewIdentifier];
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    NSInteger sections = 1;
    
    return sections;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    NSInteger rows = [[self getDefaultData] count];
    
    return rows;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 50;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell * cell = nil;
    EditAddressCell * tempCell  = [tableView dequeueReusableCellWithIdentifier:EditAddressCellIdentifier];
    [self confightCell:tempCell withIndexPath:indexPath];
    cell = tempCell;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

- (void)confightCell:(EditAddressCell *)tempCell  withIndexPath:(NSIndexPath *)indexPath{
    
    
    BOOL enabled = NO;
    NSArray * datas = [self getDefaultData];
    NSString * title = datas[indexPath.row][@"title"];
    NSString * placeholder = datas[indexPath.row][@"placeholder"];
    NSString * content = @"";
    if (indexPath.row == 2) {
        enabled = NO;
    }else{
        enabled = YES;
    }
    
    if (self.style == AddressListStyle_edit) {
        switch (indexPath.row) {
            case 0:
                content = self.consignee;
                break;
            case 1:
                 content = self.phone;
                break;
            case 2:
                 content = self.location;
                break;
            case 3:
                 content = self.detail_address;
                break;
            default:
                break;
        }
    }
    [tempCell setupTitle:title withPlaceholder:placeholder withContent:content];
    tempCell.indexPath = indexPath;
    tempCell.changeBlock = ^(NSString *inputText, NSIndexPath *indexPath) {
        switch (indexPath.row) {
            case 0:
                self.consignee = inputText;
                break;
            case 1:
                self.phone = inputText;
                break;
            case 2:
                self.location = inputText;
                break;
            case 3:
                self.detail_address = inputText;
                break;
            default:
                break;
        }
    };
    [tempCell setupTFEnabled:enabled];
}
- (NSArray *)getDefaultData{
    NSArray * tempArray = @[
                            @{@"title":@"收货人",@"placeholder":@"姓名"},
                            @{@"title":@"手机号",@"placeholder":@"手机号"},
                            @{@"title":@"地址",@"placeholder":@"选择收货地址"},
                            @{@"title":@"门牌号",@"placeholder":@"例：5号楼203室"}
                            ];
    return tempArray;
    
}
- (nullable UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView * footerView = nil;
    EditAddressFooterView * tempView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:EditAddressFooterViewIdentifier];
    tempView.saveBlock = ^{
        [self changeAddNewAddress];
    };
    footerView = tempView;
    return footerView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 80;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.row == 2) {
        [self gotoChooseAddress];
    }
}
- (void)gotoChooseAddress{
    ChooseAddressViewController * tempVC = [[ChooseAddressViewController alloc]init];
    [self pushViewController:tempVC];
}


- (void)changeAddNewAddress{
    
    AddressModel * model = self.addressModel;
    NSMutableDictionary * params = [NSMutableDictionary dictionary];
    if (self.style == AddressListStyle_edit) {
        
        [params addEntriesFromDictionary: @{
                                            @"token":UserInfo.userToken,
                                            @"address_id":model.address_id,
                                            @"consignee":self.consignee,
                                            @"phone":self.phone,
                                            @"location":self.location,
                                            @"detail_address":self.detail_address,
                                            @"is_default":self.addressModel.is_default
                                            }];
        
    }else{
        [params addEntriesFromDictionary: @{
                                            @"token":UserInfo.userToken,
                                            @"consignee":self.consignee,
                                            @"phone":self.phone,
                                            @"location":self.location,
                                            @"detail_address":self.detail_address,
                                            @"is_default":@"0"
                                            }];
    }

    [[MainApi sharedInstance] postPath:@"User/addOrEditAddress" params:params resultBlock:^(NSDictionary *responseObject, NSError *error) {
        if (!error) {
            [[NSNotificationCenter defaultCenter] postNotificationName:@"UPDATE_ADDRESS_NOTIFICATION" object:nil];
             [self backViewController];
        }
        NSLog(@"%@===",error);
    }];
    
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
